
<template>
<div>
<div class="title2">轮播图管理</div>
<div class="searchForm">
  <el-form :inline="true" :model="params" class="demo-form-inline">
  <el-form-item label="标题">
    <el-input
      v-model="params.title"
      placeholder="按标题查找"
      class="block"
    ></el-input>
  </el-form-item>
  <el-form-item>
    <el-button
      type="primary"
      icon="el-icon-search"
      @click="getList(1)"
      class="block"
    >查询</el-button>
     <el-button
      type="success"
      icon="el-icon-plus"
      @click="add"
    >增加</el-button>
     <el-button
      type="danger"
      icon="el-icon-delete"
      @click="removeMore"
    >批量删除</el-button>
  </el-form-item>
  </el-form>
</div>

<!-- 内容 -->
  <el-table
    :data="rows"
    stripe
    class="Table"      
    @selection-change="handleSelectionChange">
    <el-table-column
      type="selection"
      label="选择"
              >
    </el-table-column>
    <el-table-column

      prop="img"
      label="图片"
              >
               <template slot-scope="scope">
        <img style="max-width:50px;max-height:50px;" v-if="scope.row.img" :src="scope.row.img" />
        </template>
    </el-table-column>
    <el-table-column
      prop="title"
      label="标题"        >
    </el-table-column>
    <el-table-column
      prop="url"
      label="跳转地址"        >
    </el-table-column>
       <el-table-column
      prop="status"
      label="是否启用">
      <template slot-scope="scope">
        <el-switch
          v-model="scope.row.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="statusChange(scope.row)"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </template>
    </el-table-column>
    <el-table-column
      label="操作"
      width="320">
      <template slot-scope="scope">
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-edit"
          style="margin-right:16px;"
          @click="edit(scope.row)"
        >编辑</el-button>
         <el-popconfirm
          title="确定删除此数据吗?"
          @confirm="remove(scope.row)"
         >
          <el-button slot="reference"
          type="danger"
          size="mini"
          icon="el-icon-delete"
          >删除</el-button>
         </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
<div class="pagination">
  <el-pagination
      @size-change="sizeChange"
      @current-change="getList"
      :current-page="params.pageNum"
      :page-sizes="[5,10, 100, 200, 300, 400]"
      :page-size="params.pageSize" 
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
</div>

<el-dialog :title="form._id ? '修改' : '新增'" :visible.sync="dialogFormVisible" width="400px">
    <el-form :model="form" ref="form" :rules="rules" label-width="100px">
       <el-form-item label="选中">
        <template slot-scope="scope">
          <el-checkbox v-model="scope.row.checked"></el-checkbox>
        </template>
      </el-form-item>
      <el-form-item label="图片" prop="img">
        
  <div @click="openFile" style="overflow:hidden;width:100px;height:100px;font-size:32px;border:1px dashed #ccc;text-align:center;line-height:100px;">
           <img style="max-width:100%;max-height:100%;" v-if="form.img" :src="form.img"/>
           <span v-else>+</span></div>
          
       
         
        <input ref="file" style="display:none;" multiple accept="image/*" type="file" @change="changeFile" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input type="text" v-model="form.title"></el-input>
      </el-form-item>
       <el-form-item label="跳转地址" prop="url">
        <el-input type="text" v-model="form.url"></el-input>
      </el-form-item>
       <el-form-item label="类型" prop="type">
         <el-select v-model="form.type" placeholder="请选择">
          <el-option
            v-for="type in types"
            :key="type.value" 
            :label="type.label"
            :value="type.value">
    </el-option>
         </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </el-dialog>
</div>
</template>
<script>
import api from '@/api'
export default {
  name: 'shouyeBanner',
  data () {
    return {
      
      params:{
        title:'',
        pageNum:1,
        pageSize:5,
        type:'banner'
      },
      rows:[],
      total:0,
      dialogFormVisible:false,
      form:{ 
        _id:0,
        title:'',
        type:'banner',
        img:'',
        url:''
      },
      rules:{
       img:[
         { required: true, message: '图片不能为空', trigger: 'blur'}
         ]
      },
      types:[
        {label:'轮播图',value:'banner'},
        {label:'分类区',value:'category'},
        {label:'为你推荐',value:'recommend'}
      ],
      selected:[]
   
      }
  },
   methods: {
     getList(pageNum){
       if(pageNum) this.params.pageNum = pageNum
        this.$http({ 
          url:'/admin/index',
          params:this.params
          }
         ).then(res => {
        this.total = res.total
        this.rows = res.rows
        })
     },
     statusChange(row){
       this.$http({
         url:'/admin/index',
         method:'PUT',
         data:{
           indexId:row._id,
           status:row.status
         }
       }).then(res => {
         if(res.errMsg){
           this.getList()
         }
       })
     },
      sizeChange(pageSize){
        this.params.pageSize = pageSize
        this.getList(1)
      },
      //新增
    add(){
      this.form = { 
        _id:0,
        title:'',
        type:'banner',
        img:'',
        url:''
      }
      this.dialogFormVisible = true
    },
    //修改
    edit(row){
      this.form = row
      this.dialogFormVisible = true
    },
      changeFile(e){
      let files = e.target.files
      let formData = new FormData()
      formData.append('file',files[0])

      this.$http({
        url:'/upload',
        method:'POST',
        data:formData
      }).then(res => {
          console.log(res.path)
          var path = res.path.split("\\");
          path = path[0] + "/" + path[1];
           console.log(path)
        if(path) this.form.img = api.baseURL +'/'+ path
      })

      
    },
    openFile(){
      let fileElement =  this.$refs.file
      fileElement.click()
    },
    //删除
    remove(row){
      this.$http({
        url:'/admin/index',
        method:'DELETE',
        params:{indexId:row._id}
      }).then(res => {
        if(!res.errMsg){
          this.getList(1)
        }
      })
    },
    handleSelectionChange(rows){
      //存储选中数据
      this.selected = rows
    },
    //批量删除
    removeMore(){
      let checkedRowsPrmose = []
      let success = [], fail = []
      for(let i = 0; i< this.selected.length; i++){
        checkedRowsPrmose.push(
          new Promise(resovle => {
            let row = this.selected[i]
            this.$http({
              url:'/admin/index',
              method:'DELETE',
              params:{indexId:row._id}
            }).then(res => {
              if(!res.errorMsg){
                success.push(row._id)
              }else{
                fail.push(row._id)
              }
              resovle()
            })    
          })
        )
      }
      this.$confirm('此操作将删除选中的数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //删除
          Promise.all(checkedRowsPrmose).then(() => {
            this.$message({
              message:`批量删除完成,成功删除${success.length}条数据,删除失败${fail.length}条数据`,
              duration:3000
            });
            this.getList(1)
          })
        })
    },
    submit(){
      this.$refs.form.validate(valid => {
          if(valid){
        this.form.indexId = this.form._id
        this.$http({
        url:'/admin/index',
        method:this.form.indexId ? 'PUT' : 'POST',//通过
        data:this.form
      }).then(res => {
        if(!res.errMsg){
          this.dialogFormVisible = false
          this.getList(this.form.indexId ? undefined : 1)
        }
      })
          }
      })
     
    }
  },
 
  created(){
    this.getList()
  

  },

  mounted(){

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
